package com.atguigu.service.impl;

import com.atguigu.entity.ProductImage;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.ProductSalePropertyValue;
import com.atguigu.entity.ProductSpu;
import com.atguigu.mapper.ProductSpuMapper;
import com.atguigu.service.ProductImageService;
import com.atguigu.service.ProductSalePropertyKeyService;
import com.atguigu.service.ProductSalePropertyValueService;
import com.atguigu.service.ProductSpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-18
 */
@Service
public class ProductSpuServiceImpl extends ServiceImpl<ProductSpuMapper, ProductSpu> implements ProductSpuService {

    @Autowired
    private ProductImageService imageService;
    @Autowired
    private ProductSalePropertyKeyService salePropertyKeyService;
    @Autowired
    private ProductSalePropertyValueService  salePropertyValueService;


    @Override
    @Transactional
    public void saveProductSpu(ProductSpu productSpu) {
        //a.保存productSpu信息
        baseMapper.insert(productSpu);

        String productName = productSpu.getProductName();
        List<ProductImage> productImageList = productSpu.getProductImageList();
        if(!CollectionUtils.isEmpty(productImageList)){

            Integer num = new Integer(0);
            for (ProductImage productImage : productImageList) {
                ++num;
                String ImageName= productName+"_"+num+".jpg";
                productImage.setImageName(ImageName);
                productImage.setProductId(productSpu.getId());
            }
            //2.批量保存productImage信息
            imageService.saveBatch(productImageList);
        }
        List<ProductSalePropertyKey> salePropertyKeyList = productSpu.getSalePropertyKeyList();
        if(!CollectionUtils.isEmpty(salePropertyKeyList)){
            for (ProductSalePropertyKey salePropertyKey : salePropertyKeyList) {
                salePropertyKey.setProductId(productSpu.getId());
                List<ProductSalePropertyValue> salePropertyValueList = salePropertyKey.getSalePropertyValueList();
                if(!CollectionUtils.isEmpty(salePropertyValueList)){
                    for (ProductSalePropertyValue salePropertyValue : salePropertyValueList) {
                        salePropertyValue.setProductId(productSpu.getId());
                        salePropertyValue.setSalePropertyKeyName(salePropertyKey.getSalePropertyKeyName());
                    }
                    //3.批量保存销售属性key和value信息
                    salePropertyValueService.saveBatch(salePropertyValueList);
                }
            }
            //4.批量保存销售属性key信息
            salePropertyKeyService.saveBatch(salePropertyKeyList);
        }
    }

}


